<template>
  <div class="node-wrap">
    <!--    审批节点-->
    <!--    节点框框-->
    <div class="node-wrap-box" @click="show">
      <div class="title" style="background: #ff943e">
        <el-icon class="icon"><el-icon-user-filled /></el-icon>
        <span>{{ nodeConfig.nodeName }}</span>
        <el-icon class="close" @click.stop="delNode()"><el-icon-close /></el-icon>
      </div>
      <div class="content">
        <span v-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
        <span v-else class="placeholder">请选择</span>
      </div>
      <div class="title" style="background: #1c96b4" v-if="nodeConfig.copyPerson">
        <el-icon class="icon"><el-icon-user-filled /></el-icon>
        <span>抄送人</span>
        <el-icon class="close" @click.stop="delNode()"><el-icon-close /></el-icon>
      </div>
      <div class="content" v-if="nodeConfig.copyPerson">
        <span>{{ toText(nodeConfig.copyPerson) }}</span>
      </div>
    </div>
    <!--    带加号的节点按钮-->
    <add-node v-model="nodeConfig.childNode"></add-node>
    <!--    右侧抽屉的节点设置-->
    <el-drawer
      title="审批人设置"
      v-model="drawer"
      destroy-on-close
      append-to-body
      :size="500"
    >
      <template #header>
        <div class="node-wrap-drawer__title">
          <label @click="editTitle" v-if="!isEditTitle"
            >{{ form.nodeName
            }}<el-icon class="node-wrap-drawer__title-edit"><el-icon-edit /></el-icon
          ></label>
          <el-input
            v-if="isEditTitle"
            ref="nodeTitle"
            v-model="form.nodeName"
            clearable
            @blur="saveTitle"
            @keyup.enter="saveTitle"
          ></el-input>
        </div>
      </template>
      <el-container>
        <el-main style="padding: 0 20px 20px 20px">
          <el-form label-position="top">
            <el-form-item label="审批人员类型">
              <el-select v-model="form.setType">
                <el-option v-for="(setType, index) in setTypeList"
                           :key="setType.dictKey" :value="setType.dictKey" :label="setType.dictValue"></el-option>
<!--                <el-option :value="3" label="指定角色"></el-option>-->
<!--                <el-option :value="8" label="指定组织"></el-option>-->
<!--                <el-option :value="5" label="发起人本人审核"></el-option>-->
<!--                <el-option :value="9" label="发起人上级领导审核"></el-option>-->
<!--                <el-option :value="10" label="上级领导审核"></el-option>-->
<!--                <el-option :value="11" label="发起人所属单位审核"></el-option>-->
<!--                <el-option :value="12" label="发起人所属单位的上级单位审核"></el-option>-->
<!--                <el-option :value="13" label="上级单位审核"></el-option>-->
<!--                <el-option :value="14" label="指定接口获取审核人员"></el-option>-->
<!--                <el-option :value="15" label="用户配置审核人员"></el-option>-->
                <!-- <el-option :value="2" label="主管"></el-option> -->
                <!-- <el-option :value="4" label="发起人自选"></el-option> -->
                <!-- <el-option :value="7" label="连续多级主管"></el-option> -->
              </el-select>
            </el-form-item>

            <el-form-item v-if="form.setType === 1" label="选择成员">
              <el-button
                type="primary"
                icon="el-icon-plus"
                round
                @click="selectHandle(1, form.nodeUserList)"
                >选择人员</el-button
              >
              <div class="tags-list">
                <el-tag
                  v-for="(user, index) in form.nodeUserList"
                  :key="user.id"
                  closable
                  @close="delUser(index)"
                  >{{ user.name }}</el-tag
                >
              </div>
            </el-form-item>

            <el-form-item v-if="form.setType === 2" label="指定主管">
              发起人的第 <el-input-number v-model="form.examineLevel" :min="1" /> 级主管
            </el-form-item>

            <el-form-item v-if="form.setType === 3" label="选择角色">
              <el-button
                type="primary"
                icon="el-icon-plus"
                round
                @click="selectHandle(3, form.nodeRoleList)"
                >选择角色</el-button
              >
              <div class="tags-list">
                <el-tag
                  v-for="(role, index) in form.nodeRoleList"
                  :key="role.id"
                  type="info"
                  closable
                  @close="delRole(index)"
                  >{{ role.name }}</el-tag
                >
              </div>
            </el-form-item>

            <el-form-item v-if="form.setType === 4" label="发起人自选">
              <el-radio-group v-model="form.selectMode">
                <el-radio :value="1">自选一个人</el-radio>
                <el-radio :value="2">自选多个人</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item v-if="form.setType === 7" label="连续主管审批终点">
              <el-radio-group v-model="form.directorMode">
                <el-radio :value="0">直到最上层主管</el-radio>
                <el-radio :value="1">自定义审批终点</el-radio>
              </el-radio-group>
              <p v-if="form.directorMode === 1">
                直到发起人的第
                <el-input-number v-model="form.directorLevel" :min="1" /> 级主管
              </p>
            </el-form-item>

            <el-form-item v-if="form.setType === 8" label="选择组织">
              <el-button
                type="primary"
                icon="el-icon-plus"
                round
                @click="selectHandle(8, form.nodeOrgList)"
                >选择组织</el-button
              >
              <div class="tags-list">
                <el-tag
                  v-for="(org, index) in form.nodeOrgList"
                  :key="org.id"
                  type="info"
                  closable
                  @close="delOrg(index)"
                  >{{ org.name }}</el-tag
                >
              </div>
            </el-form-item>

            <el-form-item v-if="form.setType === 14" label="配置获取审核人员的接口">
              <el-input
                  v-model="form.funcCodeForActor"
                  placeholder="请填写funcCode"
              />
            </el-form-item>
            <div v-if="showExamineMode">
              <el-divider></el-divider>
              <el-form-item label="多人审批时审批方式">
                <el-radio-group v-model="form.examineMode" :disabled="nodeDisabled">
                  <p style="width: 100%"><el-radio :value="1">按顺序依次审批</el-radio></p>
                  <p style="width: 100%"><el-radio :value="2">会签 (可同时审批，每个人必须审批通过)</el-radio></p>
                  <p style="width: 100%"><el-radio :value="3">或签 (有一人审批通过即可)</el-radio></p>
                </el-radio-group>
              </el-form-item>
            </div>

            <div>
              <el-divider></el-divider>
              <el-form-item label="审核操作配置项">
                <el-checkbox-group v-model="form.operationConfigListForAudit">
                  <el-checkbox label="同意" value="complete" disabled/>
                  <el-checkbox label="拒绝" value="forceComplete" disabled/>
                  <el-checkbox label="撤销" value="revoke" disabled/>
                  <el-checkbox label="驳回" value="reject" />
                  <el-checkbox label="转办" value="transfer" />
                  <el-checkbox label="委派" value="delegate" />
                  <el-checkbox label="加签" value="appendNodeAfter" />
                </el-checkbox-group>
              </el-form-item>
            </div>

            <div>
              <el-divider></el-divider>
              <el-form-item label="指定审批人员方式">
                <el-radio-group v-model="form.actorWay">
                  <el-radio :value="1">人工指定</el-radio>
                  <el-radio :value="2">程序默认指定</el-radio>
                </el-radio-group>
              </el-form-item>
              <div v-if="form.actorWay === 1">
                <div style="margin-bottom: 18px">
                  <el-form-item label="指定类别">
                    <el-radio-group v-model="form.actorWayType">
                      <el-radio :value="1">按人员指定</el-radio>
                      <el-radio :value="2">按组织指定</el-radio>
                      <el-radio :value="3">按角色指定</el-radio>
                      <el-radio :value="4">按自定义指定</el-radio>
                      <el-radio :value="5">按程序接口指定</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
                <div style="margin-bottom: 18px" v-if="[1,2,4].indexOf(form.actorWayType) > -1">
                  <el-form-item label="组织范围配置">
                    <el-radio-group v-model="form.orgScope">
                      <el-radio :value="1">根据自身权限划分</el-radio>
                      <el-radio :value="2">所有组织</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
                <div style="margin-bottom: 18px" v-if="[3,4].indexOf(form.actorWayType) > -1">
                  <el-form-item label="角色范围配置">
                    <el-radio-group v-model="form.roleScope">
                      <el-radio :value="1">根据自身权限划分</el-radio>
                      <el-radio :value="2">所有角色</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </div>
            </div>

            <el-divider></el-divider>
            <el-form-item label="">
              <el-checkbox v-model="form.termAuto" label="超时自动审批"></el-checkbox>
            </el-form-item>
            <template v-if="form.termAuto">
              <el-form-item label="审批期限（为 0 则不生效）">
                <el-input-number v-model="form.term" :min="0" /> 小时
              </el-form-item>
              <el-form-item label="审批期限超时后执行">
                <el-radio-group v-model="form.termMode">
                  <el-radio :value="1">自动通过</el-radio>
                  <el-radio :value="0">自动结束</el-radio>
                </el-radio-group>
              </el-form-item>
            </template>
            <el-divider></el-divider>
            <el-form-item label="配置业务方法">
              <el-input
                v-model="form.funcCode"
                placeholder="请填写funcCode,为空状态下不调用自定义业务方法"
              />
            </el-form-item>
            <el-divider></el-divider>
            <el-form-item label="pc配置还原页面">
              <el-input
                style="margin-bottom: 14px"
                v-model="form.formUrl"
                placeholder="请填写formUrl地址"
              />
              <el-radio-group v-model="form.formMode">
                <p style="width: 100%">
                  <el-radio :value="1">仅加载当前页面组件</el-radio>
                </p>
                <p style="width: 100%">
                  <el-radio :value="2">按节点顺序叠加页面组件</el-radio>
                </p>
              </el-radio-group>
            </el-form-item>
            <el-divider></el-divider>
            <el-form-item label="app配置还原页面">
              <el-input
                  style="margin-bottom: 14px"
                  v-model="form.appFormUrl"
                  placeholder="请填写appFormUrl地址"
              />
            </el-form-item>
            <el-divider></el-divider>
            <assigner-select title="抄送人配置" ref="assigner" :examineModeFlag="false" :type="2"></assigner-select>
          </el-form>
        </el-main>
        <el-footer>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button @click="drawer = false">取消</el-button>
        </el-footer>
      </el-container>
    </el-drawer>

<!--    自定义抽屉-->
    <userConfig v-model="this.form" :drawer="drawerForUserConfig" @update:drawerForUserConfig="handleDrawerForUserConfig"></userConfig>
  </div>
</template>

<script>
import addNode from "./addNode.vue";
import assignerSelect from "@/views/system/workflow/flowTask/component/small/assignerSelect.vue";
import userConfig from '@/components/scWorkflow/custom/userConfig.vue';

export default {
  inject: ["select"],
  props: {
    modelValue: { type: Object, default: () => {} },
  },
  components: {
    addNode,assignerSelect
  },
  data() {
    return {
      nodeConfig: {},
      drawer: false,
      drawerForUserConfig: false,
      isEditTitle: false,
      form: {},
      setTypeList: [
        {dictKey: 1,dictValue: '指定成员'},
        {dictKey: 3,dictValue: '指定角色'},
        {dictKey: 8,dictValue: '指定组织'},
        {dictKey: 5,dictValue: '发起人本人审核'},
        {dictKey: 9,dictValue: '发起人上级领导审核'},
        {dictKey: 10,dictValue: '上级领导审核'},
        {dictKey: 11,dictValue: '发起人所属单位审核'},
        {dictKey: 12,dictValue: '发起人所属单位的上级单位审核'},
        {dictKey: 13,dictValue: '上级单位审核'},
        {dictKey: 14,dictValue: '指定接口获取审核人员'},
        {dictKey: 15,dictValue: '用户配置审核人员'},
      ],
    };
  },
  watch: {
    modelValue() {
      this.nodeConfig = this.modelValue;
    },
    'form.actorWay': function (newVal, oldVal) {
      if(newVal === 1){
        if(!this.form.actorWayType){
          this.form.actorWayType = 1;
        }
        if(!this.form.orgScope){
          this.form.orgScope = 1;
        }
        if(!this.form.roleScope){
          this.form.roleScope = 1;
        }
      }
    }
  },
  mounted() {
    const params = {
      dictFields: 'setType',
    };
    $api.common.getBatchDict.post(params).then((res) => {
      if(res.result.setType && res.result.setType.length > 0){
        res.result.setType.forEach(ele=>{
          ele.dictKey = Number(ele.dictKey);
        })
      }
      this.setTypeList = res.result.setType;
    });
    this.nodeConfig = this.modelValue;
  },
  methods: {
    show() {
      this.form = {};
      this.form = JSON.parse(JSON.stringify(this.nodeConfig));
      if(this.form.customDesignForUserConfig){
        this.drawerForUserConfig = true;
        return;
      }
      if(this.form.nodeOrgList === undefined){
        this.form.nodeOrgList = []
      }
      this.drawer = true;
      this.$nextTick(()=> {
        if(!this.form.copyPerson){
          this.form.copyPerson = {
            nodeUserList: [],
            nodeRoleList: [],
            nodeOrgList: [],
            setType: 1,
            examineMode: 1
          }
        }
        this.$refs.assigner.setData(this.form.copyPerson)
      })
    },
    editTitle() {
      this.isEditTitle = true;
      this.$nextTick(() => {
        this.$refs.nodeTitle.focus();
      });
    },
    saveTitle() {
      this.isEditTitle = false;
    },
    save() {
      Object.assign(this.form,{copyPerson: this.$refs.assigner.getData()})
      this.$emit("update:modelValue", this.form);
      this.drawer = false;
    },
    handleDrawerForUserConfig(drawer,nodeConfig) {
      if(drawer === false){
        this.drawerForUserConfig = drawer;
        this.$emit("update:modelValue", nodeConfig);
      }
    },
    delNode() {
      this.$emit("update:modelValue", this.nodeConfig.childNode);
    },
    delUser(index) {
      this.form.nodeUserList.splice(index, 1);
    },
    delRole(index) {
      this.form.nodeRoleList.splice(index, 1);
    },
    delOrg(index) {
      this.form.nodeOrgList.splice(index, 1);
    },
    selectHandle(type, data) {
      this.select(type, data);
    },
    toText(nodeConfig) {
      if (nodeConfig.setType == 1) {
        if (nodeConfig.nodeUserList && nodeConfig.nodeUserList.length > 0) {
          const users = nodeConfig.nodeUserList.map((item) => item.name).join("、");
          return users;
        } else {
          return false;
        }
      }else if (nodeConfig.setType == 3) {
        if (nodeConfig.nodeRoleList && nodeConfig.nodeRoleList.length > 0) {
          const roles = nodeConfig.nodeRoleList.map((item) => item.name).join("、");
          return "角色-" + roles;
        } else {
          return false;
        }
      }else if (nodeConfig.setType == 8) {
        if (nodeConfig.nodeOrgList && nodeConfig.nodeOrgList.length > 0) {
          const orgs = nodeConfig.nodeOrgList.map((item) => item.name).join("、");
          return "组织-" + orgs;
        } else {
          return false;
        }
      }
      for (let typeListElement of this.setTypeList) {
        if(typeListElement.dictKey == nodeConfig.setType){
          return typeListElement.dictValue;
        }
      }
      // if (nodeConfig.setType == 1) {
      //   if (nodeConfig.nodeUserList && nodeConfig.nodeUserList.length > 0) {
      //     const users = nodeConfig.nodeUserList.map((item) => item.name).join("、");
      //     return users;
      //   } else {
      //     return false;
      //   }
      // } else if (nodeConfig.setType == 2) {
      //   return nodeConfig.examineLevel == 1
      //     ? "直接主管"
      //     : `发起人的第${nodeConfig.examineLevel}级主管`;
      // } else if (nodeConfig.setType == 3) {
      //   if (nodeConfig.nodeRoleList && nodeConfig.nodeRoleList.length > 0) {
      //     const roles = nodeConfig.nodeRoleList.map((item) => item.name).join("、");
      //     return "角色-" + roles;
      //   } else {
      //     return false;
      //   }
      // } else if (nodeConfig.setType == 4) {
      //   return "发起人自选";
      // } else if (nodeConfig.setType == 5) {
      //   return "发起人本人审核";
      // } else if(nodeConfig.setType == 9){
      //   return "发起人上级领导审核";
      // }else if(nodeConfig.setType == 9){
      //   return "发起人上级领导审核";
      // }else if(nodeConfig.setType == 10){
      //   return "上级领导审核";
      // }else if(nodeConfig.setType == 11){
      //   return "发起人所属单位审核";
      // }else if(nodeConfig.setType == 12){
      //   return "发起人所属单位的上级单位审核";
      // }else if(nodeConfig.setType == 13){
      //   return "上级单位审核";
      // }else if(nodeConfig.setType == 14){
      //   return "指定接口获取审核人员";
      // }else if (nodeConfig.setType == 15) {
      //   return "用户配置审核接口";
      // }else if (nodeConfig.setType == 7) {
      //   return "连续多级主管";
      // } else if (nodeConfig.setType == 8) {
      //   if (nodeConfig.nodeOrgList && nodeConfig.nodeOrgList.length > 0) {
      //     const orgs = nodeConfig.nodeOrgList.map((item) => item.name).join("、");
      //     return "组织-" + orgs;
      //   } else {
      //     return false;
      //   }
      // }
    },
  },
  computed: {
    // 计算属性的 getter
    showExamineMode: function () {
      return [1,3,8].indexOf(this.form.setType) > -1;
    },
    nodeDisabled: function (){
      if(this.form.setType === 1){
        if(!this.form.nodeUserList){
          this.form.nodeUserList = [];
        }
        if(this.form.nodeUserList.length < 2){
          this.form.examineMode = 1;
          return true;
        }
      }else if(this.form.setType === 3){
        if(!this.form.nodeRoleList){
          this.form.nodeRoleList = [];
        }
        if(this.form.nodeRoleList.length < 2){
          this.form.examineMode = 1;
          return true;
        }
      }else if(this.form.setType === 8){
        if(!this.form.nodeOrgList){
          this.form.nodeOrgList = [];
        }
        if(this.form.nodeOrgList.length < 2){
          this.form.examineMode = 1;
          return true;
        }
      }
      return false;
    }
  }
};
</script>

<style></style>
